package book;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Author admin
 * @Date 2019/4/26 8:15
 * 图书管理的工具类，用JDBC实现
 * 完成图书的增删改查操作
 */
public class BookDaoDB {
    Connection conn=JDBCUtils.getConnection();
    PreparedStatement pstmt=null;
    ResultSet rs=null;


    /**
    添加功能，将book对象保存在DB
    要求：图书名称不能重复
     */
    public int insert(Book book){
        /*首先判断DB中是否已经包含该图书名称
        * 若已包含，则返回0，表示图书名称重复了
        * 若不包含，将book添加到DB*/
           /*获取数据库连接
            获取操作sql语句的对象pstmt
             执行sql语句
              关闭
             * */
        Book b=query(book.getName());
        if (b==null){
            String sql="insert into book(name,press,author,price) value (?,?,?,?)";

            try {
                PreparedStatement pstmt=conn.prepareStatement(sql);
                pstmt.setString(1,book.getName());
                pstmt.setString(2,book.getPress());
                pstmt.setString(3,book.getAuthor());
                pstmt.setDouble(4,book.getPrice());
                int c= pstmt.executeUpdate();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return 0;
    }

    private Book query(String name){
    String sql="select * from book where name=?";
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,name);
            rs=pstmt.executeQuery();
         /*因为图书名称不能重复，所以结果集中只查询出一条语句*/
            if (rs.next()){
                /*结果集中的内容 返回*/
                int id=rs.getInt("id");
                String bookname=rs.getString("name");
                String press=rs.getString("press");
                String author=rs.getString("author");
                double price=rs.getDouble("price");
                Book book=new Book(                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               name,press,author,price);
                return book;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  null;
    }

    public void select(){
        String sql="select * from book";
        try {
            pstmt=conn.prepareStatement(sql);
            rs=pstmt.executeQuery();
            while (rs.next()){
int id=rs.getInt("id");
                String name=rs.getString("name");
                String press=rs.getString("press");
                String author=rs.getString("author");
                double price=rs.getDouble("price");
                System.out.println(id+"\t"+name+"\t"+press+"\t"+author+"\t"+price);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void update() {
    }

    public void delect() {
    }
}
